package com.study.contoller;

import com.study.R;
import com.study.dto.UserInformation;
import com.study.entity.School;
import com.study.entity.User;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

/**
 * @author lxl
 * @version 2022-06-16 14:40
 */

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("selects")
    public R selects() {
        return R.ok().setData(userService.selects());
    }

    @GetMapping("selectById/{userId}")
    public R selectById(@PathVariable Long userId) {
        User user = userService.selectByPrimaryKey(userId);
//        if (user == null)
//            return R.error().setMessage("没有查询到结果");
//        else
//            return R.ok().setData(userService.selectByPrimaryKey(userId));
        return R.ok().setData(user);
    }
    
    @DeleteMapping("delete/{userId}")
    public R deleteById(@PathVariable Long userId) {
        return R.ok().setData(userService.deleteByPrimaryKey(userId));
    }
    
    @PostMapping("insert")
    public R insert(@RequestBody User user) {
        user.setUserId(UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE/1000);
        user.setUserStatus((byte) 1);
        return R.ok().setData(userService.insert(user));
    }
    
    @PutMapping("update")
    public R update(@RequestBody User user) {
        return R.ok().setData(userService.updateByPrimaryKey(user));
    }

    @GetMapping("selectAll")
    public R selectAll(UserInformation userInformation){
        return R.ok().setData(userService.selectAll(userInformation));

    }
    @PutMapping("updateKey")
    public R updateKey(@RequestBody User user) {
        return R.ok().setData(userService.updateByPrimaryKeySelective(user));
    }
}
